-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIValBox.cpp
297 lines (199 loc) · 7.18 KB
/
GUIValBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// GUIValBox.cpp
// SDL_Word_Processor
//
// Created by Nathan Daly on 7/14/12.
// Copyright 2012 Lions Entertainment. All rights reserved.
//
#ifdef _MSC_VER // necessary to use M_PI and other non-standard c/c++ definitions
# define _USE_MATH_DEFINES
#endif
#include "GUIValBox.h"
#include "GameDisplay.h"
#include "GUIApp.h"
#include "Compatibility.h"
#include SDL_SDL_H
#include <iostream>
#include <cmath>
#include TR1_FUNCTIONAL_H
using std::string;
using std::stringstream;
using std::cout; using std::endl;
using std::tr1::bind;
namespace GUI {
const int MIN_TEXT_BOX_WIDTH = 10;
const int MIN_TEXT_BOX_HEIGHT = 10;
Value_Text_Box::Value_Text_Box(int w_, int h_, string bg_image)
: Value_Box(w_,h_),
text_box(w_ - 5, h_ - 5)
{
GUIImage bg = GUIImage::create_blank(500,500);
SDL_FillRect(bg, 0, SDL_MapRGB(bg->format, 210, 210, 210));
draw_onto_self(bg, DispPoint());
attach_subview(&text_box, DispPoint(5,5));
}
double Value_Text_Box::get_value() const {
stringstream text_to_int;
text_to_int << text_box.get_text();
int result;
text_to_int >> result;
return result;
}
Value_Slider::~Value_Slider() { }
double Value_Slider::get_value() const {
return value * max - min;
}
double Value_Slider::get_percent() const {
return value;
}
const DispPoint HORIZ_SLIDER_DIM (1,5);
const DispPoint HORIZ_SLIDER_CAP_DIM (3,5);
void Value_Horiz_Slider::display() {
fill_with_color(default_color_key_c);
DispPoint loc;
loc.x = 0;
loc.y = get_h()/2 - HORIZ_SLIDER_DIM.y/2;
//Display left cap:
static GUIImage left_cap("GUIImages/slider_horiz_L.bmp", true);
draw_onto_self(left_cap, loc);
loc.x += HORIZ_SLIDER_CAP_DIM.x;
//Display center_pieces:
for (int i = 0; i < get_w()-4; i++){
static GUIImage middle("GUIImages/slider_horiz_M.bmp", true);
draw_onto_self(middle, loc);
loc.x += HORIZ_SLIDER_DIM.x;
}
//Display right cap:
static GUIImage right_cap("GUIImages/slider_horiz_R.bmp", true);
draw_onto_self(right_cap, loc);
//Dipslay Bubble
static GUIImage bubble("GUIImages/slider_bubble.bmp", true);
DispPoint position(get_percent() * get_w(), get_h()/2);
draw_onto_self(bubble, DispPoint(position.x - bubble.getw()/2,
position.y - bubble.geth()/2));
}
bool Value_Horiz_Slider::handle_mouse_down(DispPoint coord) {
Value_Slider::handle_mouse_down(coord);
set_value(static_cast<double>(coord.x) / get_w());
return true;
}
bool Value_Horiz_Slider::handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
if (!get_clicked()) return false;
if (coord.x < left_edge && get_percent() == 0) return false;
if (coord.x > right_edge && get_percent() == 1) return false;
set_value(double(coord.x) / get_w());
if (get_percent() < 0) {
set_value(0);
left_edge = coord.x;
}
if (get_percent() > 1) {
set_value(1);
right_edge = coord.x;
}
return true;
}
void Value_Vert_Slider::display() {
fill_with_color(default_color_key_c);
static GUIImage bubble("GUIImages/slider_bubble.bmp", true);
DispPoint position(get_w()/2, get_percent() * get_h());
draw_onto_self(bubble, DispPoint(position.x - bubble.getw()/2,
position.y - bubble.geth()/2));
}
bool Value_Vert_Slider::handle_mouse_down(DispPoint coord) {
Value_Slider::handle_mouse_down(coord);
set_value(static_cast<double>(coord.y) / get_w());
capture_focus();
return true;
}
bool Value_Vert_Slider::handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
if (!get_clicked()) return false;
if (coord.y < bottom_edge && get_percent() == 0) return false;
if (coord.y > top_edge && get_percent() == 1) return false;
set_value(get_percent() + static_cast<double>(rel_motion.y) / get_h());
if (get_percent() < 0) {
set_value(0);
bottom_edge = coord.y;
}
if (get_percent() > 1) {
set_value(1);
top_edge = coord.y;
}
return true;
}
void Value_Joystick_Slider::display() {
fill_with_color(default_color_key_c);
DispPoint center(get_w()/2, get_h()/2);
//Display left cap:
static GUIImage left_cap("GUIImages/slider_horiz_L.bmp", true);
draw_onto_self(left_cap, DispPoint(get_w()/2 - left_cap.getw(),
get_h()/2 - left_cap.geth()/2));
//Display right cap:
static GUIImage right_cap("GUIImages/slider_horiz_R.bmp", true);
draw_onto_self(right_cap, DispPoint(get_w()/2,
get_h()/2 - right_cap.geth()/2));
static GUIImage bubble("GUIImages/slider_bubble.bmp", true);
double radius = get_max() * get_percent();
cout << "radius: "<< radius << endl;
double radians = angle * 2*M_PI;
cout << "angle: "<< angle << endl;
cout << "radians: "<< radians << endl;
double x = radius * cos(radians);
double y = -1 * radius * sin(radians);
cout << x << endl;
cout << y << endl;
DispPoint position = center + DispPoint(x, y);
draw_onto_self(bubble, DispPoint(position.x - bubble.getw()/2,
position.y - bubble.geth()/2));
}
bool Value_Joystick_Slider::handle_mouse_down(DispPoint coord) {
Value_Slider::handle_mouse_down(coord);
DispPoint center(get_w()/2, get_h()/2);
DispPoint offset = coord - center;
double radians = atan2(-offset.y, offset.x);
if (radians < 0) { radians += 2*M_PI; } // get into range (0, 2*Pi]
angle = radians / (2*M_PI);
return true;
}
bool Value_Joystick_Slider::handle_mouse_up(DispPoint coord) {
Value_Slider::handle_mouse_up(coord);
set_value(0);
return true;
}
bool Value_Joystick_Slider::handle_mouse_motion(DispPoint coord, DispPoint rel_motion) {
if (!get_clicked()) return false;
DispPoint center(get_w()/2, get_h()/2);
DispPoint offset = coord - center;
double radians = atan2(-offset.y, offset.x);
if (radians < 0) { radians += 2*M_PI; } // get into range (0, 2*Pi]
angle = radians / (2*M_PI);
double length = sqrt((double)offset.x*offset.x + (double)offset.y*offset.y);
if (length >= get_max()) length = get_max();
// if (length > edge && get_percent() == 1) return false;
set_new_value(length);
cout << "length: "<< length << endl;
if (get_percent() < 0) {
set_value(0);
}
if (get_percent() > 1) {
set_value(1);
edge = length;
}
return true;
}
Value_Display::Value_Display(int w_, int h_, const Value_Box* linked_box)
: View(w_,h_), value_box(linked_box)
{
App::get()->repeat_on_timer(bind(&Value_Display::display, this), -1);
}
void Value_Display::display() {
fill_with_color(default_color_key_c);
set_clear_color(default_color_key_c);
if (value_box) {
set_text(value_box->get_value());
}
if (text == "") return;
SDL_Surface *text_surf = createText(text);
if (text_surf == 0) return;
draw_onto_self(GUIImage(text_surf), DispPoint(2, 2));
}
} // namespace GUI