Using sqrtf, cosf, sinf. Made STB_TEXTEDIT_ implemtations static. Tweaks.

This commit is contained in:
ocornut 2014-11-18 07:51:55 +00:00
parent 15436b46e8
commit b15532345b

View File

@ -225,7 +225,7 @@
#include "imgui.h" #include "imgui.h"
#include <ctype.h> // toupper #include <ctype.h> // toupper
#include <math.h> // sqrt #include <math.h> // sqrtf
#include <stdint.h> // intptr_t #include <stdint.h> // intptr_t
#include <stdio.h> // vsnprintf #include <stdio.h> // vsnprintf
#include <string.h> // memset #include <string.h> // memset
@ -406,7 +406,7 @@ static inline float ImSaturate(float f)
static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; } static inline float ImLerp(float a, float b, float t) { return a + (b - a) * t; }
//static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return a + (b - a) * t; } //static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return a + (b - a) * t; }
static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); } static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
static inline float ImLength(const ImVec2& lhs) { return (float)sqrt(lhs.x*lhs.x + lhs.y*lhs.y); } static inline float ImLength(const ImVec2& lhs) { return sqrtf(lhs.x*lhs.x + lhs.y*lhs.y); }
static int ImTextCharToUtf8(char* buf, size_t buf_size, unsigned int in_char); // return output UTF-8 bytes count static int ImTextCharToUtf8(char* buf, size_t buf_size, unsigned int in_char); // return output UTF-8 bytes count
static ptrdiff_t ImTextStrToUtf8(char* buf, size_t buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count static ptrdiff_t ImTextStrToUtf8(char* buf, size_t buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
@ -2414,7 +2414,7 @@ bool ImGui::Begin(const char* name, bool* open, ImVec2 size, float fill_alpha, I
window->Collapsed = (clip_rect_t.x >= clip_rect_t.z || clip_rect_t.y >= clip_rect_t.w); window->Collapsed = (clip_rect_t.x >= clip_rect_t.z || clip_rect_t.y >= clip_rect_t.w);
// We also hide the window from rendering because we've already added its border to the command list. // We also hide the window from rendering because we've already added its border to the command list.
// (we could perform the check earlier in the function but it is simplier at this point) // (we could perform the check earlier in the function but it is simpler at this point)
if (window->Collapsed) if (window->Collapsed)
window->Visible = false; window->Visible = false;
} }
@ -2604,10 +2604,10 @@ void ImGui::PopStyleVar()
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
ImGuiStyleMod& backup = window->DC.StyleModifiers.back(); ImGuiStyleMod& backup = window->DC.StyleModifiers.back();
if (float* pvar = GetStyleVarFloatAddr(backup.Var)) if (float* pvar_f = GetStyleVarFloatAddr(backup.Var))
*pvar = backup.PreviousValue.x; *pvar_f = backup.PreviousValue.x;
else if (ImVec2* pvar_t = GetStyleVarVec2Addr(backup.Var)) else if (ImVec2* pvar_v = GetStyleVarVec2Addr(backup.Var))
*pvar_t = backup.PreviousValue; *pvar_v = backup.PreviousValue;
window->DC.StyleModifiers.pop_back(); window->DC.StyleModifiers.pop_back();
} }
@ -4039,12 +4039,12 @@ bool ImGui::RadioButton(const char* label, int* v, int v_button)
} }
// Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, ASCII, fixed-width font) // Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, ASCII, fixed-width font)
int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return (int)ImStrlenW(obj->Text); } static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return (int)ImStrlenW(obj->Text); }
ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->Text[idx]; } static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->Text[idx]; }
float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { (void)line_start_idx; return obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, &obj->Text[char_idx], &obj->Text[char_idx]+1, NULL).x; } static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { (void)line_start_idx; return obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, &obj->Text[char_idx], &obj->Text[char_idx]+1, NULL).x; }
int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; }
static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static ImWchar STB_TEXTEDIT_NEWLINE = '\n';
void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx) static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx)
{ {
const ImWchar* text_remaining = NULL; const ImWchar* text_remaining = NULL;
const ImVec2 size = obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, obj->Text + line_start_idx, NULL, &text_remaining); const ImVec2 size = obj->Font->CalcTextSizeW(obj->FontSize, FLT_MAX, obj->Text + line_start_idx, NULL, &text_remaining);
@ -4060,9 +4060,9 @@ static bool is_white(unsigned int c) { return c==0 || c==' ' || c=='\t' |
static bool is_separator(unsigned int c) { return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } static bool is_separator(unsigned int c) { return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; }
#define STB_TEXTEDIT_IS_SPACE(c) (is_white((unsigned int)c) || is_separator((unsigned int)c)) #define STB_TEXTEDIT_IS_SPACE(c) (is_white((unsigned int)c) || is_separator((unsigned int)c))
void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int idx, int n) { ImWchar* dst = obj->Text+idx; const ImWchar* src = obj->Text+idx+n; while (ImWchar c = *src++) *dst++ = c; *dst = '\0'; } static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int idx, int n) { ImWchar* dst = obj->Text+idx; const ImWchar* src = obj->Text+idx+n; while (ImWchar c = *src++) *dst++ = c; *dst = '\0'; }
bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int idx, const ImWchar* new_text, int new_text_len) static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int idx, const ImWchar* new_text, int new_text_len)
{ {
ImWchar* buf_end = obj->Text + obj->BufSize; ImWchar* buf_end = obj->Text + obj->BufSize;
const size_t text_len = ImStrlenW(obj->Text); const size_t text_len = ImStrlenW(obj->Text);
@ -4391,6 +4391,8 @@ bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputT
s += bytes_count; s += bytes_count;
if (c == '\n' || c == '\r') if (c == '\n' || c == '\r')
continue; continue;
if (c >= 0x10000)
continue;
clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;
} }
clipboard_filtered[clipboard_filtered_len] = 0; clipboard_filtered[clipboard_filtered_len] = 0;
@ -5325,8 +5327,8 @@ void ImDrawList::AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, i
for (int i = 0; i < IM_ARRAYSIZE(circle_vtx); i++) for (int i = 0; i < IM_ARRAYSIZE(circle_vtx); i++)
{ {
const float a = ((float)i / (float)IM_ARRAYSIZE(circle_vtx)) * 2*PI; const float a = ((float)i / (float)IM_ARRAYSIZE(circle_vtx)) * 2*PI;
circle_vtx[i].x = (float)cos(a + PI); circle_vtx[i].x = cosf(a + PI);
circle_vtx[i].y = (float)sin(a + PI); circle_vtx[i].y = sinf(a + PI);
} }
circle_vtx_builds = true; circle_vtx_builds = true;
} }
@ -5464,7 +5466,7 @@ void ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int nu
for (int i = 0; i < num_segments; i++) for (int i = 0; i < num_segments; i++)
{ {
const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step; const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step;
AddVtxLine(centre + offset + ImVec2((float)cos(a0),(float)sin(a0))*radius, centre + ImVec2((float)cos(a1),(float)sin(a1))*radius, col); AddVtxLine(centre + offset + ImVec2(cosf(a0), sinf(a0))*radius, centre + ImVec2(cosf(a1), sinf(a1))*radius, col);
a0 = a1; a0 = a1;
} }
} }
@ -5482,8 +5484,8 @@ void ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col,
for (int i = 0; i < num_segments; i++) for (int i = 0; i < num_segments; i++)
{ {
const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step; const float a1 = (i + 1) == num_segments ? 0.0f : a0 + a_step;
AddVtx(centre + offset + ImVec2((float)cos(a0),(float)sin(a0))*radius, col); AddVtx(centre + offset + ImVec2(cosf(a0), sinf(a0))*radius, col);
AddVtx(centre + offset + ImVec2((float)cos(a1),(float)sin(a1))*radius, col); AddVtx(centre + offset + ImVec2(cosf(a1), sinf(a1))*radius, col);
AddVtx(centre + offset, col); AddVtx(centre + offset, col);
a0 = a1; a0 = a1;
} }
@ -6566,7 +6568,7 @@ void ImGui::ShowTestWindow(bool* open)
{ {
refresh_time = ImGui::GetTime(); refresh_time = ImGui::GetTime();
static float phase = 0.0f; static float phase = 0.0f;
values[values_offset] = (float)cos(phase); values[values_offset] = cosf(phase);
values_offset = (values_offset+1)%values.size(); values_offset = (values_offset+1)%values.size();
phase += 0.10f*values_offset; phase += 0.10f*values_offset;
} }