#include "ball.hpp"
#include "../properties/resources.hpp"
using namespace ball;
void main_form::main() {
application::run(main_form());
}
main_form::main_form() {
settings_form_.settings_changed += {*this, &main_form::on_settings_changed};
on_settings_changed(settings_form_, event_args::empty);
icon(properties::resources::ball_ico());
start_position(form_start_position::manual);
auto working_area = screen::from_control(*this).working_area();
location({working_area.left() + working_area.width / 2 -
size().width / 2, working_area.bottom() -
size().height});
top_most(true);
const auto fps = 60;
animation_timer_.interval_milliseconds(1000 / fps);
animation_timer_.tick += {*this, &main_form::on_animation_timer_tick};
main_panel_.context_menu(context_menu_);
main_panel_.parent(*this);
main_panel_.mouse_down +=
delegate_(
object& sender,
const mouse_event_args& e) {on_mouse_down(e);};
main_panel_.mouse_up +=
delegate_(
object& sender,
const mouse_event_args& e) {on_mouse_up(e);};
main_panel_.mouse_move +=
delegate_(
object& sender,
const mouse_event_args& e) {on_mouse_move(e);};
}
form::on_mouse_down(e);
if (is_dragging_ ||
e.button() == mouse_buttons::right)
return;
is_dragging_ = true;
velocity = {0, 0};
mouse_location_ =
e.location();
cursor(cursors::hand());
animation_timer_.enabled(false);
}
form::on_mouse_move(e);
if (!is_dragging_) return;
auto new_location = bounds().location() + (
e.location() - mouse_location_);
auto working_area = screen::from_control(*this).working_area();
new_location.x = math::clamp(new_location.x, working_area.left(), working_area.right() -
width());
new_location.y = math::clamp(new_location.y, working_area.top(), working_area.bottom() -
height());
last_mouse_move_location_ =
e.location();
}
form::on_mouse_up(e);
if (!is_dragging_ ||
e.button() == mouse_buttons::right)
return;
is_dragging_ = false;
cursor(cursors::default_cursor());
velocity =
e.location() - last_mouse_move_location_;
animation_timer_.enabled(true);
}
void main_form::on_animation_timer_tick(object& sender, const event_args& e) {
const auto gravity = 0.5f;
const auto friction = 0.98f;
const auto bounce = 0.6f;
auto bounds = this->bounds();
auto working_area = screen::from_control(*this).working_area();
velocity.y = velocity.y + gravity;
velocity.x = velocity.x * friction;
velocity.y = velocity.y * friction;
bounds.location(bounds.location() + point::truncate(velocity));
if (bounds.bottom() >= working_area.bottom()) {
bounds.y = working_area.bottom() - bounds.height;
velocity.y = -velocity.y * bounce;
}
if (bounds.left() < working_area.left()) {
bounds.x = working_area.left();
velocity.x = -velocity.x * bounce;
} else if (bounds.right() > working_area.right()) {
bounds.x = working_area.right() - bounds.width;
velocity.x = -velocity.x * bounce;
}
if (math::abs(velocity.x) < 0.1 && math::abs(velocity.y) < 0.5 && bounds.bottom() >= working_area.bottom())
animation_timer_.enabled(false);
}
main_form::close();
}
color(settings_form_.settings().color());
opacity(settings_form_.settings().opacity());
light_point_color(settings_form_.settings().light_point_color());
size(settings_form_.settings().size());
location({
left(), screen::from_control(*this).working_area().bottom() -
size().height});
}
settings_form_.show();;
}
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.hpp:18
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
#define delegate_
The declaration of a delegate type is similar to a method signature. It has a return value and any nu...
Definition delegate.hpp:932
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ e
The E key.
Definition console_key.hpp:96
@ left
Specifies the xtd::drawing::pen is positioned to the left of the theoretical line.
Definition pen_alignment.hpp:29