Skip to content

Commit

Permalink
Merge pull request OpenLoco#5 from marijnvdwerf/oct18/windows-1
Browse files Browse the repository at this point in the history
Decompile select vehicle window functions
  • Loading branch information
marijnvdwerf committed Nov 1, 2018
2 parents 0f8c186 + daa78e3 commit 3de12ac
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/openloco/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace openloco::input

bool is_pressed(ui::WindowType type, ui::window_number number, ui::widget_index index);

bool is_tool_active(ui::WindowType, ui::window_number);
void cancel_tool();
void cancel_tool(ui::WindowType, ui::window_number);

bool has_key_modifier(key_modifier modifier);
Expand Down
21 changes: 16 additions & 5 deletions src/openloco/input/mouse_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,26 @@ namespace openloco::input
return _pressedWidgetIndex == index;
}

void cancel_tool(ui::WindowType type, ui::window_number number)
bool is_tool_active(ui::WindowType type, ui::window_number number)
{
if (!has_flag(input_flags::tool_active))
return false;

return (*_toolWindowType == type && _toolWindowNumber == number);
}

// 0x004CE3D6
void cancel_tool()
{
call(0x004CE3D6);
}

void cancel_tool(ui::WindowType type, ui::window_number number)
{
if (!is_tool_active(type, number))
return;

if (*_toolWindowType == type && _toolWindowNumber == number)
{
call(0x004CE3D6);
}
cancel_tool();
}

#pragma mark - Mouse input
Expand Down
1 change: 1 addition & 0 deletions src/openloco/interop/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ void openloco::interop::register_hooks()
ui::prompt_browse::register_hooks();
ui::textinput::register_hooks();
ui::tooltip::register_hooks();
ui::vehicle::registerHooks();
ui::WindowManager::registerHooks();

register_hook(
Expand Down
5 changes: 4 additions & 1 deletion src/openloco/things/vehicle.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "../company.h"
#include "../objects/vehicle_object.h"
#include "thing.h"

Expand All @@ -14,7 +15,9 @@ namespace openloco
#pragma pack(push, 1)
struct vehicle : thing_base
{
uint8_t pad_20[0x28 - 0x20];
uint8_t pad_20;
company_id_t owner; // 0x21
uint8_t pad_22[0x28 - 0x22];
uint16_t var_28;
uint8_t pad_2A[0x2C - 0x2A];
uint16_t var_2C;
Expand Down
29 changes: 29 additions & 0 deletions src/openloco/ui/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,33 @@ namespace openloco::ui::WindowManager
}
}
}

bool isInFront(ui::window* w)
{
for (auto window = w; window < _windowsEnd; window++)
{
if ((window->flags & window_flags::stick_to_front) != 0)
continue;

return false;
}

return true;
}

bool isInFrontAlt(ui::window* w)
{
for (auto window = w; window < _windowsEnd; window++)
{
if ((window->flags & window_flags::stick_to_front) != 0)
continue;

if (window->type == WindowType::buildVehicle)
continue;

return false;
}

return true;
}
}
7 changes: 7 additions & 0 deletions src/openloco/ui/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace openloco::ui::WindowManager
void sub_4B93A5(window_number number);
void closeTopmost();
void allWheelInput();
bool isInFront(ui::window* w);
bool isInFrontAlt(ui::window* w);
}

namespace openloco::ui::windows
Expand Down Expand Up @@ -106,3 +108,8 @@ namespace openloco::ui::tooltip
void open(ui::window* window, int32_t widgetIndex, int16_t x, int16_t y);
void update(ui::window* window, int32_t widgetIndex, string_id stringId, int16_t x, int16_t y);
}

namespace openloco::ui::vehicle
{
void registerHooks();
}
2 changes: 1 addition & 1 deletion src/openloco/ui/WindowType.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace openloco::ui
title_logo = 22,
vehicle = 23,
station = 24,

dragVehiclePart = 25,
company = 26,
vehicleList = 27,
buildVehicle = 28,
Expand Down
174 changes: 174 additions & 0 deletions src/openloco/windows/vehicle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "../companymgr.h"
#include "../input.h"
#include "../interop/interop.hpp"
#include "../openloco.h"
#include "../things/thingmgr.h"
#include "../ui/WindowManager.h"

using namespace openloco::interop;

namespace openloco::ui::vehicle
{
namespace main
{
static void onUpdate(window* w);
}
namespace vehicle_details
{
static void onUpdate(window* w);
}
namespace finances
{
static void onUpdate(window* w);
}

static loco_global<window_event_list, 0x005003C0> _mainEvents;
static loco_global<window_event_list, 0x00500434> _vehicleDetailsEvents;
static loco_global<window_event_list, 0x00500554> _financesEvents;

static loco_global<int32_t, 0x0113614E> _113614E;
static loco_global<int16_t, 0x01136156> _1136156;

void registerHooks()
{
_mainEvents->on_update = main::onUpdate;
_vehicleDetailsEvents->on_update = vehicle_details::onUpdate;
_financesEvents->on_update = finances::onUpdate;
}

static void sub_4B28E2(window* w, int dx)
{
registers regs;

regs.dx = dx;
regs.esi = (uintptr_t)w;

call(0x004B28E2, regs);
}

namespace main
{
// 0x004B30F3
static void onUpdate(window* w)
{
w->frame_no += 1;
w->call_prepare_draw();

WindowManager::invalidateWidget(WindowType::vehicle, w->number, 4);
WindowManager::invalidateWidget(WindowType::vehicle, w->number, 10);
WindowManager::invalidateWidget(WindowType::vehicle, w->number, 13);
WindowManager::invalidateWidget(WindowType::vehicle, w->number, 14);
WindowManager::invalidateWidget(WindowType::vehicle, w->number, 15);

if (w->is_disabled(13))
{
input::cancel_tool(WindowType::vehicle, w->number);
return;
}

auto vehicle = thingmgr::get<openloco::vehicle>(w->number);

if (vehicle->tile_x != -1 && (vehicle->var_38 & (1 << 4)) == 0)
{
return;
}

if (!WindowManager::isInFront(w))
return;

if (vehicle->owner != companymgr::get_controlling_id())
return;

if (!input::is_tool_active(WindowType::vehicle, w->number))
{
sub_4B28E2(w, 13);
}
}
}

namespace vehicle_details
{
// 0x004B3C45
static void onUpdate(window* w)
{
if (w->number == _1136156)
{
if (WindowManager::find(WindowType::dragVehiclePart) != nullptr)
{
_1136156 = -1;
_113614E = -1;
w->invalidate();
}
}

w->frame_no += 1;
w->call_prepare_draw();

WindowManager::invalidateWidget(WindowType::vehicle, w->number, 5);

if (_1136156 == -1 && w->is_activated(11))
{
WindowManager::invalidateWidget(WindowType::vehicle, w->number, 11);
}

if (w->is_disabled(10))
{
input::cancel_tool(WindowType::vehicle, w->number);
return;
}

auto vehicle = thingmgr::get<openloco::vehicle>(w->number);
if (vehicle->tile_x != -1 && (vehicle->var_38 & (1 << 4)) == 0)
return;

if (!WindowManager::isInFrontAlt(w))
return;

if (vehicle->owner != companymgr::get_controlling_id())
return;

if (!input::is_tool_active(WindowType::vehicle, w->number))
{
sub_4B28E2(w, 10);
}
}
}

namespace finances
{
// 0x004B55D1
static void onUpdate(window* w)
{
w->frame_no += 1;
w->call_prepare_draw();

WindowManager::invalidateWidget(WindowType::vehicle, w->number, 8);

auto vehicle = thingmgr::get<openloco::vehicle>(w->number);
if (vehicle->owner != companymgr::get_controlling_id())
return;

if (!WindowManager::isInFront(w))
return;

if (input::is_tool_active(WindowType::vehicle, w->number))
return;

registers regs;
regs.dx = 9; // tool widget
regs.al = 12;
regs.esi = (uintptr_t)w;

auto result = call(0x004CE367, regs);
bool cf = (result & (1 << 8)) != 0;
if (cf)
return;

w->invalidate();

registers regs2;
regs.esi = (uintptr_t)vehicle;
call(0x00470824, regs2);
}
}
}

0 comments on commit 3de12ac

Please sign in to comment.