mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpigui] Add GetDistSquared and MaxFit functions
These utility functions were moved from halsim_gui.
This commit is contained in:
@@ -24,7 +24,6 @@
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpigui.h>
|
||||
|
||||
#include "GuiUtil.h"
|
||||
#include "HALSimGui.h"
|
||||
#include "SimDeviceGui.h"
|
||||
#include "portable-file-dialogs.h"
|
||||
@@ -310,7 +309,7 @@ FieldFrameData FieldInfo::GetFrameData() const {
|
||||
|
||||
// fit the image into the window
|
||||
if (m_texture && m_imageHeight != 0 && m_imageWidth != 0)
|
||||
MaxFit(&ffd.imageMin, &ffd.imageMax, m_imageWidth, m_imageHeight);
|
||||
gui::MaxFit(&ffd.imageMin, &ffd.imageMax, m_imageWidth, m_imageHeight);
|
||||
|
||||
ImVec2 min = ffd.imageMin;
|
||||
ImVec2 max = ffd.imageMax;
|
||||
@@ -324,7 +323,7 @@ FieldFrameData FieldInfo::GetFrameData() const {
|
||||
}
|
||||
|
||||
// draw the field "active area" as a yellow boundary box
|
||||
MaxFit(&min, &max, m_width, m_height);
|
||||
gui::MaxFit(&min, &max, m_width, m_height);
|
||||
|
||||
ffd.min = min;
|
||||
ffd.max = max;
|
||||
@@ -579,15 +578,15 @@ static void DisplayField2D() {
|
||||
if (ImGui::IsItemHovered()) {
|
||||
float hitRadiusSquared = hitRadius * hitRadius;
|
||||
// it's within the hit radius of the center?
|
||||
if (GetDistSquared(cursor, rfd.center) < hitRadiusSquared)
|
||||
if (gui::GetDistSquared(cursor, rfd.center) < hitRadiusSquared)
|
||||
hit = 1;
|
||||
else if (GetDistSquared(cursor, rfd.corners[0]) < hitRadiusSquared)
|
||||
else if (gui::GetDistSquared(cursor, rfd.corners[0]) < hitRadiusSquared)
|
||||
hit = 2;
|
||||
else if (GetDistSquared(cursor, rfd.corners[1]) < hitRadiusSquared)
|
||||
else if (gui::GetDistSquared(cursor, rfd.corners[1]) < hitRadiusSquared)
|
||||
hit = 3;
|
||||
else if (GetDistSquared(cursor, rfd.corners[2]) < hitRadiusSquared)
|
||||
else if (gui::GetDistSquared(cursor, rfd.corners[2]) < hitRadiusSquared)
|
||||
hit = 4;
|
||||
else if (GetDistSquared(cursor, rfd.corners[3]) < hitRadiusSquared)
|
||||
else if (gui::GetDistSquared(cursor, rfd.corners[3]) < hitRadiusSquared)
|
||||
hit = 5;
|
||||
if (hit > 0 && ImGui::IsMouseClicked(0)) {
|
||||
if (hit == 1) {
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "GuiUtil.h"
|
||||
|
||||
void halsimgui::MaxFit(ImVec2* min, ImVec2* max, float width, float height) {
|
||||
float destWidth = max->x - min->x;
|
||||
float destHeight = max->y - min->y;
|
||||
if (width == 0 || height == 0) return;
|
||||
if (destWidth * height > destHeight * width) {
|
||||
float outputWidth = width * destHeight / height;
|
||||
min->x += (destWidth - outputWidth) / 2;
|
||||
max->x -= (destWidth - outputWidth) / 2;
|
||||
} else {
|
||||
float outputHeight = height * destWidth / width;
|
||||
min->y += (destHeight - outputHeight) / 2;
|
||||
max->y -= (destHeight - outputHeight) / 2;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace halsimgui {
|
||||
|
||||
// get distance^2 between two ImVec's
|
||||
inline float GetDistSquared(const ImVec2& a, const ImVec2& b) {
|
||||
float deltaX = b.x - a.x;
|
||||
float deltaY = b.y - a.y;
|
||||
return deltaX * deltaX + deltaY * deltaY;
|
||||
}
|
||||
|
||||
// maximize fit while preserving aspect ratio
|
||||
void MaxFit(ImVec2* min, ImVec2* max, float width, float height);
|
||||
|
||||
} // namespace halsimgui
|
||||
@@ -413,4 +413,19 @@ bool gui::CreateTextureFromImage(const unsigned char* data, int len,
|
||||
return true;
|
||||
}
|
||||
|
||||
void gui::MaxFit(ImVec2* min, ImVec2* max, float width, float height) {
|
||||
float destWidth = max->x - min->x;
|
||||
float destHeight = max->y - min->y;
|
||||
if (width == 0 || height == 0) return;
|
||||
if (destWidth * height > destHeight * width) {
|
||||
float outputWidth = width * destHeight / height;
|
||||
min->x += (destWidth - outputWidth) / 2;
|
||||
max->x -= (destWidth - outputWidth) / 2;
|
||||
} else {
|
||||
float outputHeight = height * destWidth / width;
|
||||
min->y += (destHeight - outputHeight) / 2;
|
||||
max->y -= (destHeight - outputHeight) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -351,4 +351,29 @@ class Texture {
|
||||
int m_height = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get square of distance between two ImVec2's.
|
||||
*
|
||||
* @param a first ImVec
|
||||
* @param b second ImVec
|
||||
*
|
||||
* @return Distance^2 between a and b.
|
||||
*/
|
||||
inline float GetDistSquared(const ImVec2& a, const ImVec2& b) {
|
||||
float deltaX = b.x - a.x;
|
||||
float deltaY = b.y - a.y;
|
||||
return deltaX * deltaX + deltaY * deltaY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximize fit in "window" while preserving aspect ratio. Min and max
|
||||
* passed-in values are modified such that the object maximally fits.
|
||||
*
|
||||
* @param min upper left corner of window (modified to fit)
|
||||
* @param max lower right corner of window (modified to fit)
|
||||
* @param width width of object to fit
|
||||
* @param height height of object to fit
|
||||
*/
|
||||
void MaxFit(ImVec2* min, ImVec2* max, float width, float height);
|
||||
|
||||
} // namespace wpi::gui
|
||||
|
||||
Reference in New Issue
Block a user