Skip to content

Commit

Permalink
newui window blocker, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
afwbkbc committed Feb 9, 2025
1 parent fbb5086 commit 3dfed7d
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 40 deletions.
27 changes: 23 additions & 4 deletions GLSMAC_data/default/uidemo.gls.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,28 +556,47 @@
header_font: ':24',
header_background: 'navy',
align: 'center',
blocker: 'rgba(0, 0, 0, 127)',
});
window.text({
align: 'center',
text: 'WINDOW',
class: 'balltext',
});
window.button({
const closebtn = window.button({
class: 'button1 button2 button-active',
align: 'bottom right',
height: 30,
width: 100,
right: 10,
bottom: 10,
text: 'CLOSE',
})
.on('click', (e) => {
});
closebtn.on('click', (e) => {
window.remove();
window = null;
return true;
})
;
const blockbtn = window.button({
class: 'button1 button2',
align: 'bottom left',
height: 30,
width: 100,
left: 10,
bottom: 10,
text: 'BLOCK',
});
blockbtn.on('click', (e) => {
if (window.blocker == #undefined) {
window.blocker = 'rgba(0, 0, 0, 120)';
blockbtn.text = 'UNBLOCK';
}
else {
window.blocker = #undefined;
blockbtn.text = 'BLOCK';
}
return true;
});
}
return true;
});
Expand Down
6 changes: 5 additions & 1 deletion src/GLSMAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ GLSMAC::~GLSMAC() {

m_gse->GetAsync()->StopTimers();

DELETE( m_ui );
{
gse::ExecutionPointer ep;
m_ui->Destroy( m_ctx, { "" }, ep );
}

DELETE( m_gse );

s_glsmac = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/audio/sdl2/SDL2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ void SDL2::Stop() {
return;
}

free( m_buffer );
free( m_mix_buffer );

Log( "Deinitializing SDL2" );
SDL_CloseAudio();

free( m_buffer );
free( m_mix_buffer );

}

void SDL2::Iterate() {
Expand Down
4 changes: 3 additions & 1 deletion src/graphics/opengl/actor/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ void Cache::UpdateCacheImpl( shader_program::ShaderProgram* shader_program, scen
m_texture, tl, br, [ this, &shader_program, &camera ]() {
for ( const auto& it : m_cache_children_by_zindex ) {
for ( const auto& child : it.second ) {
child->DrawImpl( shader_program, camera );
if ( child->GetActor()->IsVisible() ) {
child->DrawImpl( shader_program, camera );
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scene/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ CLASS( Entity, types::Serializable )
virtual const types::Buffer Serialize() const override;
virtual void Unserialize( types::Buffer buf ) override;

void Show();
void Hide();
virtual void Show();
virtual void Hide();
const bool IsVisible() const;

protected:
Expand Down
14 changes: 14 additions & 0 deletions src/scene/actor/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ const Cache* const Actor::GetCacheParent() const {
return m_cache_parent;
}

void Actor::Show() {
if ( !m_is_visible ) {
Entity::Show();
UpdateCache();
}
}

void Actor::Hide() {
if ( m_is_visible ) {
Entity::Hide();
UpdateCache();
}
}

const types::Buffer Actor::Serialize() const {
types::Buffer buf = Entity::Serialize();

Expand Down
3 changes: 3 additions & 0 deletions src/scene/actor/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ CLASS( Actor, Entity )
void SetCacheParent( Cache* const cache_parent );
const Cache* const GetCacheParent() const;

void Show();
void Hide();

virtual const types::Buffer Serialize() const override;
virtual void Unserialize( types::Buffer buf ) override;

Expand Down
7 changes: 5 additions & 2 deletions src/ui/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ UI::~UI() {

g_engine->GetInput()->RemoveHandler( this );

delete m_root;

for ( const auto& it : m_classes ) {
Log( "Destroying UI class: " + it.first );
delete it.second;
Expand Down Expand Up @@ -187,6 +185,11 @@ const types::Vec2< ssize_t >& UI::GetLastMousePosition() const {
return m_last_mouse_position;
}

void UI::Destroy( GSE_CALLABLE ) {
m_root->Destroy( GSE_CALL );
delete this;
}

void UI::AddIterable( const dom::Object* const obj, const f_iterable_t& f ) {
ASSERT( m_iterables.find( obj ) == m_iterables.end(), "iterable already exists" );
m_iterables.insert( {obj, f } );
Expand Down
2 changes: 2 additions & 0 deletions src/ui/UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ CLASS2( UI, common::Class, gse::Wrappable )

const types::Vec2< ssize_t >& GetLastMousePosition() const;

void Destroy( GSE_CALLABLE );

private:
friend class dom::Object;
typedef std::function< void() > f_iterable_t;
Expand Down
18 changes: 12 additions & 6 deletions src/ui/dom/Area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ Area::Area( DOM_ARGS_T )

const bool Area::IsEventRelevant( const input::Event& event ) const {
if ( event.flags & input::EF_MOUSE ) {
if ( ( event.type != input::EV_MOUSE_OUT ) && !m_geometry->Contains(
{
event.data.mouse.x,
event.data.mouse.y
}
) ) {
if (
event.type != input::EV_MOUSE_OUT &&
(
!m_is_visible ||
!m_geometry->Contains(
{
event.data.mouse.x,
event.data.mouse.y
}
)
)
) {
return false;
}
}
Expand Down
30 changes: 23 additions & 7 deletions src/ui/dom/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void Container::ForwardFactories( GSE_CALLABLE, Object* child ) {
}
}

void Container::Embed( Object* object ) {
void Container::Embed( Object* object, const bool is_visible ) {
ASSERT_NOLOG( !m_is_initialized, "container already initialized" );
m_embedded_objects.push_back( object );
m_embedded_objects.push_back( { object, is_visible } );
}

Container::~Container() {}
Expand All @@ -106,13 +106,20 @@ void Container::UpdateMouseOver( GSE_CALLABLE ) {
for ( auto it = m_children.rbegin() ; it != m_children.rend() ; it++ ) { // later children have priority
auto* object = it->second;
const auto* geometry = object->GetGeometry();
if ( geometry && geometry->Contains( mouse_coords ) ) {
if ( object->m_is_visible && geometry && geometry->Contains( mouse_coords ) ) {
SetMouseOverChild( GSE_CALL, object, mouse_coords );
m_processing_mouse_overs = false;
return;
}
}
if ( m_mouse_over_object && ( !m_is_mouse_over || !m_mouse_over_object->GetGeometry()->Contains( mouse_coords ) ) ) {
if (
m_mouse_over_object &&
(
!m_is_mouse_over ||
!m_mouse_over_object->m_is_visible ||
!m_mouse_over_object->GetGeometry()->Contains( mouse_coords )
)
) {
SetMouseOverChild( GSE_CALL, nullptr, mouse_coords );
}
}
Expand All @@ -133,7 +140,7 @@ const bool Container::ProcessEvent( GSE_CALLABLE, const input::Event& event ) {
break;
}
case input::EV_MOUSE_MOVE: {
m_is_mouse_over = m_geometry->Contains( { event.data.mouse.x, event.data.mouse.y } );
m_is_mouse_over = m_is_visible && m_geometry->Contains( { event.data.mouse.x, event.data.mouse.y } );
UpdateMouseOver( GSE_CALL );
break;
}
Expand All @@ -153,7 +160,11 @@ const bool Container::ProcessEvent( GSE_CALLABLE, const input::Event& event ) {
m_is_processing_children_events = false;
ProcessPendingDeletes( GSE_CALL );
auto result = Area::ProcessEvent( GSE_CALL, event );
if ( ( event.flags & input::EF_MOUSE ) && m_geometry->Contains( { event.data.mouse.x, event.data.mouse.y }) ) {
if (
( event.flags & input::EF_MOUSE ) &&
m_is_visible &&
m_geometry->Contains( { event.data.mouse.x, event.data.mouse.y } )
) {
result = true; // only one child can handle mouse event, regardless of processing result
}
return result;
Expand Down Expand Up @@ -250,6 +261,7 @@ void Container::Factory( GSE_CALLABLE, const std::string& name, const std::funct
}
auto* obj = f( GSE_CALL, initial_properties );
ASSERT_NOLOG( obj, "object not created" );
obj->Show();
obj->InitAndValidate( GSE_CALL );
m_children.insert({ obj->m_id, obj });
return obj->Wrap( true );
Expand All @@ -265,7 +277,11 @@ void Container::OnPropertyRemove( GSE_CALLABLE, const std::string& key ) const {
}

void Container::InitAndValidate( GSE_CALLABLE ) {
for ( const auto& obj : m_embedded_objects ) {
for ( const auto& it : m_embedded_objects ) {
auto* obj = it.first;
if ( m_is_visible && it.second ) {
obj->Show();
}
obj->InitAndValidate( GSE_CALL );
m_children.insert({ obj->m_id, obj });
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/dom/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Container : public Area {
Container( DOM_ARGS_T, const bool factories_allowed );

void ForwardFactories( GSE_CALLABLE, Object* child );
void Embed( Object* object );
void Embed( Object* object, const bool is_visible = true );

scene::actor::Cache* const m_cache;

Expand Down Expand Up @@ -54,7 +54,7 @@ class Container : public Area {
bool m_is_processing_children_events = false;

std::map< id_t, Object* > m_children = {};
std::vector< Object* > m_embedded_objects = {};
std::vector< std::pair< Object*, bool > > m_embedded_objects = {};
std::map< std::string, std::pair< Object*, std::string > > m_forwarded_properties = {};

void InitAndValidate( GSE_CALLABLE ) override;
Expand Down
16 changes: 15 additions & 1 deletion src/ui/dom/Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,19 @@ const bool Drawable::ProcessEvent( GSE_CALLABLE, const input::Event& event ) {
return Object::ProcessEvent( GSE_CALL, event );
}

void Drawable::Show() {
if ( !m_is_visible ) {
Object::Show();
m_geometry->Show();
}
}

void Drawable::Hide() {
if ( m_is_visible ) {
m_geometry->Hide();
Object::Hide();
}
}

}
}
}
3 changes: 3 additions & 0 deletions src/ui/dom/Drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Drawable : public Object {

virtual const bool ProcessEvent( GSE_CALLABLE, const input::Event& event ) override;

void Show() override;
void Hide() override;

protected:

virtual ~Drawable();
Expand Down
24 changes: 23 additions & 1 deletion src/ui/dom/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Object::Object( DOM_ARGS_T )
}

Object::~Object() {
Hide();
if ( m_is_iterable_set ) {
m_ui->RemoveIterable( this );
}
Expand Down Expand Up @@ -161,8 +162,29 @@ void Object::Destroy( GSE_CALLABLE ) {
delete this;
}

void Object::Show() {
if ( !m_is_visible ) {
for ( const auto& actor : m_actors ) {
actor->Show();
}
m_is_visible = true;
}
}

void Object::Hide() {
if ( m_is_visible ) {
for ( const auto& actor : m_actors ) {
actor->Hide();
}
m_is_visible = false;
}
}

const bool Object::IsEventRelevant( const input::Event& event ) const {
return m_supported_events.find( event.type ) != m_supported_events.end();
return
m_is_visible &&
m_supported_events.find( event.type ) != m_supported_events.end()
;
}

const bool Object::ProcessEventImpl( GSE_CALLABLE, const input::Event& event ) {
Expand Down
7 changes: 6 additions & 1 deletion src/ui/dom/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Object : public gse::Wrappable {

virtual void Destroy( GSE_CALLABLE );

virtual void Show();
virtual void Hide();

protected:

virtual ~Object();
Expand Down Expand Up @@ -97,7 +100,9 @@ class Object : public gse::Wrappable {

void AddModifier( GSE_CALLABLE, const class_modifier_t modifier );
void RemoveModifier( GSE_CALLABLE, const class_modifier_t modifier );


bool m_is_visible = false;

private:

std::unordered_set< input::event_type_t > m_supported_events = {};
Expand Down
2 changes: 2 additions & 0 deletions src/ui/dom/Root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Root::Root( GSE_CALLABLE, UI* const ui )
FACTORY( "window", Window );

InitAndValidate( GSE_CALL );

Show();
}

void Root::Resize( const uint16_t window_width, const uint16_t window_height ) {
Expand Down
Loading

0 comments on commit 3dfed7d

Please sign in to comment.