MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TextPanel.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  TextPanel.h Panel for text content
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef TEXTPANEL_H
28 #define TEXTPANEL_H
29 
30 #include "itype.h"
31 #include "Panel.h"
32 #include "LineTailByteSink.h"
33 
34 namespace MFM {
39  template <u32 COLUMNS, u32 LINES>
40  class TextPanel : public Panel
41  {
42  private: typedef Panel Super;
43 
44  public:
45  TextPanel()
46  {
47  SetName("Text Panel");
48  SetRenderPoint(SPoint(0, 0));
49  SetForeground(Drawing::GREEN);
50  SetBackground(Drawing::GREY05);
51  SetBorder(Drawing::GREY20);
52 
53  m_bottomLineShown = 0;
54  }
55 
56  ByteSink & GetByteSink() {
57  return m_text;
58  }
59 
60  protected:
61  virtual void PaintComponent(Drawing& drawing)
62  {
63  Super::PaintComponent(drawing);
64  RenderText(drawing);
65  }
66 
67  virtual bool Handle(MouseButtonEvent& mbe)
68  {
69  SDL_MouseButtonEvent & event = mbe.m_event.button;
70  if(event.type == SDL_MOUSEBUTTONDOWN) {
71 
73  pt.Set(event.x - pt.GetX(),
74  event.y - pt.GetY());
75 
76  switch (event.button) {
77 
78  case SDL_BUTTON_LEFT:
79  if (pt.GetX() < (s32) ELEVATOR_WIDTH) {
80  m_leftButtonDragStart = SPoint(event.x,event.y);//pt;
81  m_dragStartElevatorBottom = m_elevatorBottom;
82  }
83  break;
84 
85  case SDL_BUTTON_WHEELUP:
86  case SDL_BUTTON_WHEELDOWN:
87  s32 dir = event.button == SDL_BUTTON_WHEELDOWN ? -1 : 1;
88  if (mbe.m_keyboard.ShiftHeld()) dir *= 10;
89 
90  m_bottomLineShown = MAX<s32>(0, MIN<s32>(m_bottomLineShown + dir, m_text.GetLines()));
91  break;
92  }
93  }
94 
95  return true;
96  }
97 
98  virtual bool Handle(MouseMotionEvent& mbe)
99  {
100  SDL_MouseMotionEvent & event = mbe.m_event.motion;
101 
103  pt.Set(event.x - pt.GetX(),
104  event.y - pt.GetY());
105 
106  if ((pt.GetX() < (s32) ELEVATOR_WIDTH) &&
107  (mbe.m_buttonMask & (1 << SDL_BUTTON_LEFT)) !=0) {
108  SPoint nowAt(event.x, event.y);
109  s32 deltay = nowAt.GetY() - m_leftButtonDragStart.GetY();
110  m_bottomLineShown = ElevatorBottomToBottomLine(m_dragStartElevatorBottom + deltay);
111  }
112 
113  // Whether we did anything or not, we're eating the move here
114  return true;
115  }
116 
117  private:
118  static const u32 ELEVATOR_WIDTH = 12;
119  static const u32 ELEVATOR_COLOR = 0xff007f00;
120 
122  u32 m_bottomLineShown;
123  u32 m_lastBytesWritten;
124 
125  SPoint m_leftButtonDragStart;
126  u32 m_dragStartElevatorBottom;
127 
128  u32 m_panelHeight;
129  u32 m_fontHeight;
130  u32 m_neededHeight;
131  u32 m_textLines;
132  u32 m_elevatorRange;
133  u32 m_elevatorBottom;
134  u32 m_elevatorHeight;
135 
136  // If we move elevatorBottom this many pixels, how many bottom lines
137  // is that, based on the most-recent prior render?
138  s32 ElevatorBottomToBottomLine(s32 newElevatorBottom) {
139  // Don't wrap
140  newElevatorBottom = MAX<s32>(0, MIN<s32>(newElevatorBottom, m_panelHeight - m_elevatorHeight));
141 
142  s32 newBot = m_textLines - newElevatorBottom * m_textLines / m_elevatorRange;
143  return MAX<s32>(0, MIN<s32>(newBot, m_text.GetLines()));
144  }
145 
146  void RenderText(Drawing & drawing)
147  {
148  m_panelHeight = GetHeight();
149  if (m_panelHeight == 0)
150  m_panelHeight = 1; // WTF:(
151 
152  TTF_Font * font = drawing.GetFont();
153  if (!font) return; // WTF?
154 
155  m_fontHeight = TTF_FontLineSkip(font);
156  if (m_fontHeight == 0)
157  m_fontHeight = 1; // WTF!
158 
159  // Figure out size of elevator
160  m_textLines = m_text.GetLines();
161  m_neededHeight = m_textLines * m_fontHeight;
162 
163  m_elevatorHeight = m_panelHeight;
164  if (m_neededHeight > m_panelHeight)
165  m_elevatorHeight = m_panelHeight * m_panelHeight / m_neededHeight;
166 
167  // Figure out elevator position
168  if (m_lastBytesWritten != m_text.GetBytesWritten()) {
169 
170  // Snap to bottom on new output
171  m_lastBytesWritten = m_text.GetBytesWritten();
172  m_bottomLineShown = 0;
173  }
174 
175  m_elevatorRange = m_panelHeight - m_elevatorHeight;
176  m_elevatorBottom = m_elevatorRange * (m_textLines - m_bottomLineShown) / m_textLines;
177 
178  drawing.FillRect(1, m_elevatorBottom, ELEVATOR_WIDTH-2, m_elevatorHeight, ELEVATOR_COLOR);
179 
180  s32 y = m_panelHeight;
181  for (s32 line = m_textLines - m_bottomLineShown - 1; line >= 0; --line) {
182  const char * zline = m_text.GetZString(line);
183  if (!zline) {
184  printf("WTF %d\n", line);
185  }
186  drawing.BlitText(zline, UPoint(ELEVATOR_WIDTH, y), UPoint(GetWidth(), m_fontHeight));
187  y -= m_fontHeight;
188  if (y < 0) break;
189  }
190  }
191 
192  };
193 
194 } /* namespace MFM */
195 
196 #endif /* TEXTPANEL_H */
virtual bool Handle(MouseButtonEvent &mbe)
Definition: TextPanel.h:67
u32 GetLines() const
Definition: LineTailByteSink.h:123
Definition: Panel.h:115
u32 SetBorder(const u32 color)
Definition: Panel.h:238
Definition: Panel.h:91
Definition: Panel.h:74
u32 SetForeground(const u32 color)
Definition: Panel.h:256
void Set(T x, T y)
Definition: Point.tcc:183
T GetY() const
Definition: Point.tcc:40
SPoint GetAbsoluteLocation()
Definition: Panel.cpp:173
virtual void PaintComponent(Drawing &config)
Definition: Panel.cpp:230
Definition: ByteSink.h:47
u32 GetBytesWritten() const
Definition: LineTailByteSink.h:111
const char * GetZString(u32 whichLine)
Definition: LineTailByteSink.h:143
virtual void PaintComponent(Drawing &drawing)
Definition: TextPanel.h:61
virtual bool Handle(MouseMotionEvent &mbe)
Definition: TextPanel.h:98
Definition: TextPanel.h:40
Definition: Drawing.h:44
u32 SetBackground(const u32 color)
Definition: Panel.h:220
T GetX() const
Definition: Point.tcc:34