Example #1
0
void AwesomeBar::build_widgets() { 
    set_margin_left(20);
    set_margin_right(20);
    set_margin_top(20);
    set_margin_bottom(20);
    set_no_show_all();
    set_valign(Gtk::ALIGN_START);
    set_halign(Gtk::ALIGN_CENTER);

    entry_.set_width_chars(50);
    pack_start(entry_, false, false);

    entry_.signal_changed().connect([&]() {
        populate(entry_.get_text().c_str());
    });

    entry_.signal_activate().connect(sigc::mem_fun(this, &AwesomeBar::execute));
    entry_.signal_focus_out_event().connect([=](GdkEventFocus* evt) -> bool {
        // Horrible hack on old Gtk+

        auto minor_version = gtk_get_minor_version();
        if(about_to_focus && minor_version < 12) {
            about_to_focus = false;
            entry_.grab_focus();
            entry_.set_position(-1);
            return true;
        }
        entry_.hide();
        entry_.set_text("");
        return false;
    });

    entry_.signal_key_press_event().connect([=](GdkEventKey* evt) -> bool {
        if(evt->keyval == GDK_KEY_Down || evt->keyval == GDK_KEY_Up) {
            /*
             * If we key up or down, change the selection in the list but
             * return false so that we don't lose focus on the box
             */

            auto row = list_.get_selected_row();
            if(!row) {
                return false;
            }

            auto idx = row->get_index();

            idx += (evt->keyval == GDK_KEY_Down) ? 1 : -1;

            idx = std::max(idx, 0);
            idx = std::min(idx, (int32_t) displayed_files_.size());

            about_to_focus = true;
            list_.select_row(*list_.get_row_at_index(idx));

            return true;
        }

        return false;
    });

    Pango::FontDescription desc("sans-serif 16");
    entry_.override_font(desc);

    pack_start(list_revealer_, false, true, 0);

    list_window_.set_size_request(-1, 650);

    list_.set_activate_on_single_click(true);
    list_window_.add(list_);
    list_revealer_.add(list_window_);

    list_.set_hexpand(false);

    list_.signal_row_activated().connect([this](Gtk::ListBoxRow* row){
        auto idx = row->get_index();
        auto file = displayed_files_.at(idx);
        window_.open_document(file);
        hide();
    });
}
void main_game::run(float dt) {
    game_base::run(dt);
	// world position for interactions
	world_mouse_pos = camera.viewport_to_world(mouse->position, screen_rect);

	nebular_background->run(camera);
	current_level->run(dt);

	if (state == game_state::game_base) {
		bullet_particle_system.upload(dt, screen_rect);
		sparks_particle_system.upload(dt, screen_rect);
		ship_sys.run(dt, screen_rect);
		ship_sys.calc_screen_pos(camera, screen_rect);
		background_dust->run(camera, screen_rect);
		waypoint_visualizer->run();
		trails_renderer->run(camera);

		if (ship_sys.size() > 0) {
			auto button_container = (ui::ui_container*)ui.named_elements["ship_button_container"];

			uint32_t current_button_count = 0;

			for (auto button_it = button_container->begin(); button_it != button_container->end(); ++button_it) {
				current_button_count++;
			}

			if (current_button_count < ship_sys.size()) {
				for (uint32_t i = 0; i < ship_sys.size() - current_button_count; ++i) {
					auto new_selection_button = new ship_selection_button(button_container);
				}
			}

			auto current_button = (ship_selection_button*)*button_container->begin();

			for (auto ship_it = ship_sys.begin(); ship_it != ship_sys.end(); ++ship_it) {
				auto screen_pos = (*ship_it)->screen_position;

				current_button->set_margin_left(floor(screen_pos.x - 8.f));
				current_button->set_margin_top(floor(screen_pos.y - 8.f));

				if ((*ship_it)->team == 0) {
					if (exists_in(teams[0].selected_ships, *ship_it)) {
						current_button->set_selection_state(ship_selection_button_state::selected);
					} else {
						current_button->set_selection_state(ship_selection_button_state::player);
					}

				} else {
					current_button->set_selection_state(ship_selection_button_state::enemy);
				}

				current_button->assc_ship = *ship_it;
				current_button->mouse_button_released(ui::get_ui_handler(&main_game::ship_selection_button_mouse_released, this));

				current_button = (ship_selection_button*)current_button->get_next_leaf();
			}

			while (current_button) {
				current_button->set_visibility(false);
				current_button = (ship_selection_button*)current_button->get_next_leaf();
			}

			button_container->arrange_layout(viewport);
		}

		// camera movements
		if (mouse->wheel_y > 0) {
			camera.zoom_in(dt);
		}

		if (mouse->wheel_y < 0) {
			camera.zoom_out(dt);
		}

		if (mouse->is_down(mouse_buttons::middle)) {
			camera.move(mouse->delta.reflect_y());
		}


		if (mouse->was_pressed(mouse_buttons::left)) {
			// enabled selection rect
			if (!ui.render_context.is_drawn_pixel(world_mouse_pos)) {
				selection_anchor = world_mouse_pos;
				is_selection_active = true;
				selection_renderer->show();
			}
		}

		if (is_selection_active) {
			if (!ui.render_context.is_drawn_pixel(world_mouse_pos)) {
				teams[0].previous_selected_ships.clear();
				teams[0].selected_ships.swap(teams[0].previous_selected_ships);

				// create selection rect
				selection_rect.position.x = min(selection_anchor.x, world_mouse_pos.x);
				selection_rect.position.y = min(selection_anchor.y, world_mouse_pos.y);
				selection_rect.size.width = abs(selection_anchor.x - world_mouse_pos.x);
				selection_rect.size.height = abs(selection_anchor.y - world_mouse_pos.y);
				selection_renderer->set_rect(selection_rect);

				// find ships inside selection rect
				rect ship_rect;
				for (auto& current_ship : teams[0].ships) {

					ship_rect.position.x = current_ship->transform.position.x - current_ship->dimension.size.width * 0.5f;
					ship_rect.position.y = current_ship->transform.position.y - current_ship->dimension.size.height * 0.5f;
					ship_rect.size.width = current_ship->dimension.size.width;
					ship_rect.size.height = current_ship->dimension.size.height;

					if (selection_rect.intersects(ship_rect)) {
						teams[0].selected_ships.push_back(current_ship);
					}
				}

				// check if some ship was selected
				for (auto selected_ship : teams[0].selected_ships) {
					if (!exists_in(teams[0].previous_selected_ships, selected_ship)) {
						selected_ship->select();
					}
				}

				// check if some ship was unselected
				for (auto prev_selected_ship : teams[0].previous_selected_ships) {
					if (!exists_in(teams[0].selected_ships, prev_selected_ship)) {
						prev_selected_ship->unselect();
					}
				}
			}
		}

		if (mouse->was_released(mouse_buttons::left)) {
			// disabled selection rect
			is_selection_active = false;
			selection_renderer->hide();
		}

		if (mouse->was_pressed(mouse_buttons::right)) {
			if (ui.is_drawn_pixel(mouse->position)) {

			} else {
				// order selected ships to fly to mouse position 
				waypoint new_waypoint;
				new_waypoint.type = waypoint_type::fly_to_position;
				new_waypoint.position_target = world_mouse_pos;

				add_waypoint_to_selection(new_waypoint);
			}
		}

		// damp screen shake amplitude
		screen_shake.set_amplitude(screen_shake.get_amplitude() * exp(-4.0f * dt));
	}

	if (state == game_state::loading) {
		if (!loader.is_finished()) {
			((ui_label*)ui.named_elements["loading_label"])->set_text(std::to_string((int)(loader.get_progress() * 100.f)));
			loader.load(hub, 10.f);
			loading_squares->run(loader.get_progress());
		} else {
			change_game_state(game_state::game_base);
		}
	}
}