Skip to content

Commit 4e94932

Browse files
committed
Crafting button as part of iface panel (closes #9)
- Draw button PCX directly on the iface window buffer - Use combination of get_mouse_x/y, get_game_mode and global_script_repeat to simulate all button events (except mouse on - but it's not used) - Prevent clicking if game mode is not 0 (any window is open) - Should (hopefully) finally work flawlessly regardless of HRP versions and settings
1 parent f59dc65 commit 4e94932

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

scripts_src/_pbs_craft/gl_pbs_craft.ssl

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ procedure delete_button;
2020

2121
#include "crafting.h"
2222

23-
variable crafting_hotkey, schema_pids;
24-
variable warning_msg, show_crafting_scheduled, crafting_button_shown, crafting_button_top;
23+
variable begin
24+
crafting_hotkey;
25+
schema_pids;
26+
warning_msg;
27+
show_crafting_scheduled;
28+
crafting_button_shown;
29+
crafting_button_top;
30+
31+
button_x;
32+
button_y;
33+
button_global_x;
34+
button_global_y;
35+
//iface_window_id;
36+
end
2537

2638
#define WIN_CRAFT_BTN "helwin"
2739
#define SFX_BUTTON_DOWN "butin4"
@@ -34,6 +46,7 @@ variable warning_msg, show_crafting_scheduled, crafting_button_shown, crafting_b
3446
#define BUTTON_BOTTOM_OFFSET 62
3547
#define BUTTON_RIGHT_OFFSET 399
3648

49+
#define can_enter_crafting (not(get_game_mode) and craft_ui_mode <= MODE_EXIT)
3750

3851
procedure warning_combat begin
3952
if (warning_msg == false) then begin
@@ -58,7 +71,7 @@ procedure keypress_handler begin
5871
if (hotkey_pressed_now(crafting_hotkey, key)) then begin
5972
if combat_is_initialized then
6073
call warning_combat;
61-
else if (not(get_game_mode) and craft_ui_mode <= MODE_EXIT) then begin
74+
else if (can_enter_crafting) then begin
6275
call schedule_show_crafting;
6376
end
6477
end else if (craft_ui_mode > MODE_EXIT) then begin
@@ -130,15 +143,16 @@ procedure do_up begin
130143
end
131144
end
132145

133-
// Create the button
146+
// DEPRECATED Create the button
147+
/*
134148
procedure create_button begin
135149
// Mash's HRP has a bug when clicking on main interface - it goes on top (sfall doesn't have this).
136150
// Workaround: delete and recreate button when going to/from these UI's with moveontop flag only when button is actually clickable.
137-
variable onTop := not(get_game_mode bwand (BARTER bwor INVENTORY bwor DIALOG));
151+
variable onTop := true;// not(get_game_mode bwand (BARTER bwor INVENTORY bwor DIALOG));
138152
if crafting_button_shown then begin
139-
if (crafting_button_top != onTop) then
140-
DeleteWin(WIN_CRAFT_BTN);
141-
else
153+
//if (crafting_button_top != onTop) then
154+
// DeleteWin(WIN_CRAFT_BTN);
155+
//else
142156
return;
143157
end
144158
variable
@@ -150,8 +164,41 @@ procedure create_button begin
150164
AddButtonGFX("but", PCX_BUTTON_DOWN, PCX_BUTTON_UP, PCX_BUTTON_UP);
151165
AddButtonProc("but", do_on, do_off, do_down, do_up);
152166
ShowWin;
153-
crafting_button_top := onTop;
167+
//crafting_button_top := onTop;
154168
crafting_button_shown := true;
169+
end*/
170+
171+
procedure draw_button(variable pressed) begin
172+
interface_art_draw(WINTYPE_IFACEBAR, PCX_BUTTON_DOWN if pressed else PCX_BUTTON_UP, button_x, button_y);
173+
end
174+
175+
// Create the button
176+
procedure init_button begin
177+
button_x := get_interface_width(WINTYPE_IFACEBAR) - BUTTON_RIGHT_OFFSET;
178+
button_y := get_interface_height(WINTYPE_IFACEBAR) - BUTTON_BOTTOM_OFFSET;
179+
button_global_x := get_interface_x(WINTYPE_IFACEBAR) + button_x;
180+
button_global_y := get_interface_y(WINTYPE_IFACEBAR) + button_y;
181+
// TODO: after sfall update
182+
// iface_window_id := get_interface_id(WINTYPE_IFACEBAR);
183+
call draw_button(false);
184+
end
185+
186+
procedure mouse_over_button begin
187+
// TODO: after sfall update
188+
// if (get_window_under_mouse != iface_window_id) then return false;
189+
variable x := get_mouse_x, y := get_mouse_y;
190+
return x >= button_global_x
191+
and x < button_global_x + BUTTON_WIDTH
192+
and y >= button_global_y
193+
and y < button_global_y + BUTTON_HEIGHT;
194+
end
195+
196+
procedure check_mouse_off_button begin
197+
if (mouse_over_button) then return;
198+
199+
call draw_button(false);
200+
call do_off;
201+
set_global_script_repeat(0);
155202
end
156203

157204
procedure delete_button begin
@@ -161,15 +208,40 @@ procedure delete_button begin
161208
DeleteWin(WIN_CRAFT_BTN);
162209
end
163210

211+
procedure mouseclick_handler begin
212+
variable
213+
pressed := get_sfall_arg,
214+
btn := get_sfall_arg;
215+
//window := get_window_under_mouse;
216+
217+
display_msg(string_format("mouseclick (%d, %d) mode %x", get_mouse_x, get_mouse_y, get_game_mode));
218+
if (btn == 0 and mouse_over_button and can_enter_crafting) then begin
219+
if (pressed) then begin
220+
call draw_button(true);
221+
call do_down;
222+
set_global_script_repeat(1);
223+
end
224+
else begin
225+
call draw_button(false);
226+
call do_up;
227+
end
228+
end
229+
end
230+
231+
// OLD button code
232+
/*
233+
#define OVERLAPPING_MODES (LOADGAME bwor LOCALMAP bwor WORLDMAP bwor OPTIONS bwor CHARSCREEN bwor PIPBOY bwor AUTOMAP bwor BARTER bwor DIALOG bwor SPECIAL bwor INVENTORY bwor INTFACEUSE bwor INTFACELOOT)
234+
164235
procedure gamemodechange_handler begin
165236
variable gameExit := get_sfall_arg;
166-
if (not (get_game_mode bwand (LOADGAME bwor LOCALMAP bwor WORLDMAP)) and not gameExit) then begin
237+
//display_msg(sprintf("gamemode= %x", get_game_mode));
238+
if (not (get_game_mode bwand OVERLAPPING_MODES) and not gameExit) then begin
167239
call create_button;
168240
end
169241
else begin
170242
call delete_button;
171243
end
172-
end
244+
end*/
173245

174246
procedure useobj_handler begin
175247
variable
@@ -203,19 +275,22 @@ procedure start begin
203275
crafting_hotkey := parse_hotkey(craft_raw_cfg.Main.hotkey);
204276
schema_pids := array_fixed(string_split_ints(craft_raw_cfg.Main.schematic_pids, ","));
205277
register_hook_proc(HOOK_KEYPRESS, keypress_handler);
206-
register_hook_proc(HOOK_GAMEMODECHANGE, gamemodechange_handler);
278+
register_hook_proc(HOOK_MOUSECLICK, mouseclick_handler);
279+
//register_hook_proc(HOOK_GAMEMODECHANGE, gamemodechange_handler);
207280
register_hook_proc(HOOK_DESCRIPTIONOBJ, descriptionobj_handler);
208281
register_hook_proc(HOOK_USEOBJ, useobj_handler);
209282

210-
call create_button;
283+
call init_button;
211284

212285
set_global_script_type(1);
213286
end else begin
214287
//debug_msg("gl_fixit repeats");
215288
if (show_crafting_scheduled) then begin
216289
show_crafting_scheduled := false;
217290
call show_crafting_window;
291+
set_global_script_repeat(0);
218292
end
219-
set_global_script_repeat(0);
293+
if (button_pressed) then
294+
call check_mouse_off_button;
220295
end
221296
end

0 commit comments

Comments
 (0)