Add sources.
[runemen.git] / src / ui.c
blob802e240c6bf27143abab52757a5aed6c495df3f2
1 #include <SDL.h>
2 #include "rune.h"
3 #include "runeconf.h"
4 #include "ui.h"
6 ui_t ui;
8 void reset_ui() {
9 ui.x = 0;
10 ui.y = 0;
12 ui.vx = 0;
13 ui.vy = 0;
15 ui.dragging = 0;
17 ui.unit = ui.house = ui.builder = ui.stat = ui.btn = -1;
19 ui.hintType = -1;
21 ui.hover_top = overNothingMajor;
22 ui.hover = overNothingMinor;
24 /* layers */
25 ui.draw_fog = 0;
26 ui.draw_path = 0;
27 ui.draw_pools = 0;
28 ui.draw_overlays = 0;
29 ui.draw_scent = 0;
31 /* debug */
32 ui.game_speed = 25;//PASS_DEL;
33 ui.fps = 0;
34 ui.fps_counter = 0;
37 void count_fps() {
39 static Uint32 wait = 0;
40 static Uint32 fps = 0;
42 Uint32 tick = SDL_GetTicks();
43 Uint32 pass = tick - wait;
45 if (pass >= 1000) {
46 ui.fps = ui.fps_counter;
47 ui.fps_counter = 0;
48 wait = tick;
53 void update_hintbox() {
55 switch (ui.hover) {
57 case overBuildButton:
59 ui.hintType = 0;
60 ui.hint = ui.hover_id;
61 break;
63 case overUnitStat:
65 ui.hintType = 1;
66 ui.hint = ui.hover_id;
67 break;
69 default:
70 ui.hintType = -1;
71 ui.hint = -1;
72 break;
75 /* Hacks */
76 if (ui.builder != -1) {
78 ui.hintType = 0;
79 ui.hint = ui.builder;
82 if (ui.stat != -1) {
84 ui.hintType = 1;
85 ui.hint = ui.stat;
91 void track_mouse() {
93 /* Given X and Y mouse position, determine various UI-related states... */
94 SDL_Rect game_map = { 0, 0, LOG_WIDTH - PANE_WIDTH, LOG_HEIGHT };
96 SDL_Rect ui_pane = { LOG_WIDTH - PANE_WIDTH - 10, 0, PANE_WIDTH + 10, LOG_HEIGHT };
98 SDL_Rect top_pane = { 0, 0, LOG_WIDTH, 16 };
100 ui.hover = overNothingMinor;
101 ui.hover_id = 0;
103 if (SDL_InBounds(ui.x, ui.y, &ui_pane)) {
105 ui.hover_top = overPane;
107 track_mouse_ui();
109 } else if (SDL_InBounds(ui.x, ui.y, &top_pane)) {
111 ui.hover_top = overNothingMajor;
113 } else {
115 ui.hover_top = overMap;
117 track_mouse_map();
121 update_hintbox();
124 void track_mouse_map() {
126 Uint32 unit_i, house_i;
128 ui.hover_xcollide = 0;
129 ui.hover = overNothingMinor;
131 if (ui.builder != -1) {
132 house_p *h = &bhouses[ui.builder];
134 int lx = ui.x + ui.vx;
135 int ly = ui.y + ui.vy;
137 int cx = -(TILE_W * h->w / 2) + (TILE_W/2);
138 int cy = -(TILE_H * h->h / 2) + (TILE_H/2);
140 lx += cx;
141 ly += cy;
143 ui.hover_tx = (lx / TILE_W);
144 ui.hover_ty = (ly / TILE_H);
146 ui.hover_xcollide = xcollide(ui.hover_tx, ui.hover_ty, h->w, h->h);
148 return;
151 ui.hover_tx = (ui.x + ui.vx) / TILE_W;
152 ui.hover_ty = (ui.y + ui.vy) / TILE_H;
154 unit_i = find_unit(ui.x, ui.y);
155 if (unit_i != -1) {
156 ui.hover = overUnit;
157 ui.hover_id = unit_i;
158 } else {
159 house_i = find_house(ui.x, ui.y);
160 if (house_i != -1) {
161 ui.hover = overHouse;
162 ui.hover_id = house_i;
167 void track_mouse_ui() {
169 int i;
171 /* One of the build buttons */
172 if (ui.house == -1 && ui.unit == -1) {
173 SDL_Rect button = {
174 LOG_WIDTH - PANE_WIDTH,
175 150, /* minimap_height ? */
180 for (i = 0; i < HMENU_ITEMS; i++) {
182 house_p *m = &bhouses[i];
184 if (SDL_InBounds(ui.x, ui.y, &button)) {
186 ui.hover = overBuildButton;
187 ui.hover_id = i;
189 break;
192 button.x += 17;
193 if (button.x >= LOG_WIDTH - 10) {
194 button.x = LOG_WIDTH - PANE_WIDTH;
195 button.y += 17;
200 /* One of the stat/skill buttons */
201 if (ui.unit != -1) {
202 SDL_Rect button = {
203 LOG_WIDTH - PANE_WIDTH + BOX_PADDING + BOX_PADDING,
204 150 + BOX_PADDING + BOX_PADDING + 1,
209 for (i = 0; i < MAX_STAT; i++) {
210 button.y += 14;
211 if (SDL_InBounds(ui.x, ui.y, &button)) {
212 ui.hover = overUnitStat;
213 ui.hover_id = i;
217 button.y -= 14 * MAX_STAT;
218 button.x += 64;
220 for (i = 0; i < MAX_STAT; i++) {
221 button.y += 14;
222 if (SDL_InBounds(ui.x, ui.y, &button)) {
223 ui.hover = overUnitSkill;
224 ui.hover_id = i;