void Widget::visible(bool v) { if( v != flags.visible ) { flags.visible = v; /* TODO: This on_show/on_hide implementation seems inelegant. * But I need *some* way to take/configure resources when * a widget becomes visible, and reverse the process when the * widget becomes invisible, whether the widget (or parent) is * hidden, or the widget (or parent) is removed from the tree. */ if( v ) { on_show(); } else { on_hide(); // Set all children invisible too. for(const auto child : children()) { child->visible(false); } } } }
shooting(main_window *w): window(*w), config(keyconfig::get_instance()), holder(std::make_shared<resource_holder>()), player_(holder) { register_resource(holder.get()); register_object(&dialog); dialog.set_button_text(0, L"はい"); dialog.set_button_handler(0, [this](){ this->dialog.hide(); on_hide(); on_show(); }); dialog.set_button_text(1, L"いいえ"); dialog.set_button_handler(1, [this](){ this->dialog.hide(); this->window.select_scene(main_window::scene::title); }); dialog.set_size(paint::size(300, 200)); dialog.set_position(paint::point(250, 200)); add_keyboard_handler([this](unsigned keycode, bool push){ config->set_keyboard_state(keycode, push); }, keycode_range(0, 255)); add_timer_handler([this](...){ if(pause) return; auto state = config->get_state(); vector v; const float velocity = 7.f; if(state.up){ v[1][0] -= velocity; } if(state.down){ v[1][0] += velocity; } if(state.left){ v[0][0] -= velocity; } if(state.right){ v[0][0] += velocity; } player_.set_vector(v); auto now = std::chrono::system_clock::now(); if(state.b && now - bullet_time >= std::chrono::milliseconds(192)){ auto it = std::find(bullets.begin(), bullets.end(), false); if(it != bullets.end()){ float r = it->get_radius(); it->set_position(player_.get_center()); it->set_vector(vector(0, -10)); it->set_active(); auto trans = transform::scale(1.f, 1.01f); if(state.a){ trans = transform::rotation(to_rad(-0.5)) * trans; } if(state.c){ trans = transform::rotation(to_rad(0.5)) * trans; } it->set_transform(trans); } bullet_time = now; } if(now - enemy_time >= enemy_duration){ enemy_duration = std::chrono::milliseconds(this->rand() % 750 + 250); enemy_time = now; enemies.push_back(new enemy1(holder)); enemies.back()->set_position(paint::point(static_cast<float>(this->rand() % 800), -50)); } move(); this->window.repaint(); }, timer_id); }