Skip to content

Commit 850c027

Browse files
committed
units list done
1 parent aec1cb6 commit 850c027

File tree

10 files changed

+136
-25
lines changed

10 files changed

+136
-25
lines changed

GLSMAC_data/default/ui/parts/game/bottom_bar/object_preview.gls.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ return {
5353
}
5454
if (!#is_defined(this.previews[cls])) {
5555
this.previews[cls] = this.frame.widget({
56+
class: 'bottombar-object-preview',
5657
type: type,
5758
data: data,
58-
align: 'top center',
59-
top: 12,
60-
width: 100,
61-
height: 75,
6259
});
6360
} else {
6461
this.previews[cls].data = data;
@@ -128,12 +125,23 @@ return {
128125
}
129126
},
130127

128+
show: (object) => {
129+
this.set_image(object);
130+
this.set_lines(object);
131+
},
132+
131133
init: (p) => {
132134

133135
this.p = p;
134-
135136
this.um = p.game.get_um();
136137

138+
p.ui.class('bottombar-object-preview').set({
139+
align: 'top center',
140+
top: 12,
141+
width: 100,
142+
height: 75,
143+
});
144+
137145
this.frame = p.el.panel({
138146
class: 'bottombar-frame',
139147
align: 'top left',
@@ -143,16 +151,11 @@ return {
143151
width: 129,
144152
});
145153

146-
const f_update = (object) => {
147-
this.set_image(object);
148-
this.set_lines(object);
149-
};
150-
151154
p.map.on('unit_preview', (e) => {
152-
f_update(e.unit);
155+
this.show(e.unit);
153156
});
154157
p.map.on('base_preview', (e) => {
155-
f_update(e.base);
158+
this.show(e.base);
156159
})
157160

158161
},

GLSMAC_data/default/ui/parts/game/bottom_bar/objects_list.gls.js

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
return {
22

3+
selected_unit: null,
4+
active_item: null,
5+
6+
set_active_item: (object) => {
7+
if (this.active_item != null) {
8+
this.active_item.class = 'bottombar-objects-list-item';
9+
this.active_item.border = #undefined; // TODO: fix class switching logic to also apply modifier styles correctly
10+
}
11+
this.active_item = object;
12+
if (this.active_item != null) {
13+
this.active_item.class = 'bottombar-objects-list-item-active';
14+
}
15+
},
16+
317
add_object: (object, left) => {
418
const cls = #classof(object);
519
let type = null;
@@ -33,25 +47,51 @@ return {
3347
class: 'bottombar-objects-list-item-preview',
3448
top: top,
3549
});
36-
if (cls == 'Unit') {
37-
const p = this.p; // TODO: make this work within object
38-
item.text({
39-
class: 'bottombar-objects-list-item-text',
40-
text: p.get_stats_str(object),
41-
});
50+
switch (cls) {
51+
case 'Unit': {
52+
const p = this.p; // TODO: make this work within object
53+
item.text({
54+
class: 'bottombar-objects-list-item-text',
55+
text: p.get_stats_str(object),
56+
});
57+
if (object == this.selected_unit) {
58+
this.set_active_item(item);
59+
}
60+
item.on('mousedown', (e) => {
61+
this.p.game.select_unit(object);
62+
return true;
63+
});
64+
item.on('mouseover', (e) => {
65+
this.p.sections.object_preview.show(object);
66+
return true;
67+
});
68+
item.on('mouseout', (e) => {
69+
this.p.sections.object_preview.show(this.selected_unit);
70+
return true;
71+
});
72+
break;
73+
}
74+
case 'Base': {
75+
// TODO:
76+
break;
77+
}
4278
}
4379
},
4480

45-
update_units: (tile) => {
81+
update_tile: (tile) => {
82+
4683
if (#is_defined(this.list)) {
4784
this.list.remove();
85+
this.set_active_item(null);
4886
}
4987
this.list = this.frame.scrollview({
5088
left: 2,
5189
right: 2,
5290
top: -8,
53-
bottom: -6,
91+
bottom: -8,
5492
padding: 10,
93+
has_hscroll: false,
94+
has_vscroll: false,
5595
});
5696

5797
let left = 0;
@@ -67,6 +107,7 @@ return {
67107
this.add_object(unit, left);
68108
left += this.object_width;
69109
}
110+
70111
},
71112

72113
init: (p) => {
@@ -79,7 +120,16 @@ return {
79120

80121
p.ui.class('bottombar-objects-list-item').set({
81122
width: width,
82-
height: 48,
123+
height: 52,
124+
_hover: {
125+
border: 'rgb(14,40,49),1',
126+
},
127+
});
128+
p.ui.class('bottombar-objects-list-item-active').extend('bottombar-objects-list-item').set({
129+
border: 'rgb(28,80,99),1',
130+
_hover: {
131+
border: 'rgb(28,80,99),1',
132+
},
83133
});
84134

85135
p.ui.class('bottombar-objects-list-item-preview').set({
@@ -91,6 +141,7 @@ return {
91141
font: 'arialnb.ttf:12',
92142
color: 'rgb(235,235,235)',
93143
align: 'bottom center',
144+
bottom: 3,
94145
});
95146

96147
this.frame = p.el.panel({
@@ -102,8 +153,17 @@ return {
102153
height: 58,
103154
});
104155

156+
p.map.on('unit_preview', (e) => {
157+
if (e.unit != this.selected_unit) {
158+
this.selected_unit = e.unit;
159+
if (this.selected_unit != null) {
160+
this.update_tile(this.selected_unit.get_tile());
161+
}
162+
}
163+
});
164+
105165
p.map.on('tile_preview', (e) => {
106-
this.update_units(e.tile);
166+
this.update_tile(e.tile);
107167
});
108168

109169
},

src/game/FrontendRequest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FrontendRequest {
3636
FR_ANIMATION_UNDEFINE,
3737
FR_ANIMATION_SHOW,
3838
FR_ANIMATION_ABORT,
39+
FR_UNIT_SELECT,
3940
FR_UNIT_DEFINE,
4041
FR_UNIT_UNDEFINE,
4142
FR_UNIT_SPAWN,
@@ -124,6 +125,9 @@ class FrontendRequest {
124125
struct {
125126
size_t running_animation_id;
126127
} animation_abort;
128+
struct {
129+
size_t unit_id;
130+
} unit_select;
127131
struct {
128132
const std::string* serialized_unitdef; // can be optimized
129133
} unit_define;

src/game/backend/Game.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,17 @@ WRAPIMPL_BEGIN( Game )
910910
return VALUE( gse::value::Bool,, m_game_state != GS_NONE );
911911
} )
912912
},
913+
{
914+
"select_unit",
915+
NATIVE_CALL( this ) {
916+
N_EXPECT_ARGS( 1 );
917+
N_GETVALUE_UNWRAP( unit, 0, unit::Unit );
918+
auto fr = FrontendRequest( FrontendRequest::FR_UNIT_SELECT );
919+
fr.data.unit_select.unit_id = unit->m_id;
920+
AddFrontendRequest( fr );
921+
return VALUE( gse::value::Undefined );
922+
} )
923+
},
913924
};
914925
if ( m_tm ) {
915926
properties.insert(

src/game/frontend/Game.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,14 @@ void Game::ProcessRequest( const FrontendRequest* request ) {
11471147
AbortAnimation( request->data.animation_abort.running_animation_id );
11481148
break;
11491149
}
1150+
case FrontendRequest::FR_UNIT_SELECT: {
1151+
auto* const unit = m_um->GetUnitById( request->data.unit_select.unit_id );
1152+
if ( unit && unit->IsActive() ) {
1153+
m_um->SelectUnit( unit, true );
1154+
ScrollToTile( unit->GetTile(), true );
1155+
}
1156+
break;
1157+
}
11501158
case FrontendRequest::FR_UNIT_DEFINE: {
11511159
types::Buffer buf( *request->data.unit_define.serialized_unitdef );
11521160
const auto* unitdef = backend::unit::Def::Deserialize( buf );

src/gse/Value.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ const std::string& Value::GetTypeString() const {
111111
}
112112

113113
const std::string Value::ToString() const {
114+
std::unordered_set< const Value* > stack = {};
115+
return ToStringImpl( stack );
116+
}
117+
118+
const std::string Value::ToStringImpl( std::unordered_set< const Value* >& stack ) const {
114119
switch ( type ) {
115120
case VT_UNDEFINED:
116121
return "undefined";
@@ -147,15 +152,24 @@ const std::string Value::ToString() const {
147152
std::string str = "";
148153
str.append( obj->object_class + "{ " );
149154
bool first = true;
155+
stack.insert( this );
150156
for ( const auto& it : obj->value ) {
151157
if ( first ) {
152158
first = false;
153159
}
154160
else {
155161
str.append( ", " );
156162
}
157-
str.append( it.first + ": " + it.second->ToString() );
163+
const auto* const v = it.second;
164+
str.append(
165+
it.first + ": " + (
166+
stack.find( v ) == stack.end()
167+
? it.second->ToStringImpl( stack )
168+
: "<recursion>"
169+
)
170+
);
158171
}
172+
stack.erase( this );
159173
str.append( " }" );
160174
return str;
161175
}

src/gse/Value.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class Value : public gc::Object {
234234
static const std::string& GetTypeStringStatic( const value_type_t type );
235235
const std::string& GetTypeString() const;
236236
virtual const std::string ToString() const;
237+
const std::string ToStringImpl( std::unordered_set< const Value* >& stack ) const;
237238
const std::string Dump() const;
238239

239240
Value* const Deref();

src/scene/Scene.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Scene::~Scene() {
1111
if ( m_local_camera ) {
1212
DELETE( m_local_camera );
1313
}
14+
{
15+
std::lock_guard guard( m_actors_mutex );
16+
for ( const auto& actor : m_actors ) {
17+
actor->SetScene( nullptr );
18+
}
19+
}
1420
}
1521

1622
void Scene::AddActor( actor::Actor* actor ) {

src/ui/dom/Object.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ void Object::ClearActors() {
340340
g = g->AsRectangle();
341341
}
342342
for ( const auto& it : m_actors ) {
343-
it.scene->RemoveActor( it.actor );
343+
if ( it.scene ) {
344+
it.scene->RemoveActor( it.actor );
345+
}
344346
if ( g ) {
345347
((geometry::Rectangle*)g)->Clear();
346348
}

src/ui/dom/Scrollbar.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ Scrollbar::Scrollbar( DOM_ARGS_T )
176176
ASSERT( false, "Unknown scrollbar type: " + std::to_string( m_scroll_type ) );
177177
}
178178
m_slider_drag.is_dragging = true;
179-
m_slider_drag.drag_handler_id = m_ui->AddGlobalHandler( f_drag_handler );
179+
if ( !m_slider_drag.drag_handler_id ) {
180+
m_slider_drag.drag_handler_id = m_ui->AddGlobalHandler( f_drag_handler );
181+
}
180182
}
181183
return true;
182184
};

0 commit comments

Comments
 (0)