@@ -76,6 +76,7 @@ CONSOLE_DECLARE_SYMBOL(SDL_SetRenderDrawColor);
76
76
CONSOLE_DECLARE_SYMBOL (SDL_SetTextureBlendMode);
77
77
CONSOLE_DECLARE_SYMBOL (SDL_SetTextureColorMod);
78
78
CONSOLE_DECLARE_SYMBOL (SDL_SetWindowMinimumSize);
79
+ CONSOLE_DECLARE_SYMBOL (SDL_ShowCursor);
79
80
CONSOLE_DECLARE_SYMBOL (SDL_ShowWindow);
80
81
CONSOLE_DECLARE_SYMBOL (SDL_StartTextInput);
81
82
CONSOLE_DECLARE_SYMBOL (SDL_StopTextInput);
@@ -137,6 +138,7 @@ void bind_sdl_symbols()
137
138
CONSOLE_ADD_SYMBOL (SDL_SetTextureBlendMode),
138
139
CONSOLE_ADD_SYMBOL (SDL_SetTextureColorMod),
139
140
CONSOLE_ADD_SYMBOL (SDL_SetWindowMinimumSize),
141
+ CONSOLE_ADD_SYMBOL (SDL_ShowCursor),
140
142
CONSOLE_ADD_SYMBOL (SDL_ShowWindow),
141
143
CONSOLE_ADD_SYMBOL (SDL_StartTextInput),
142
144
CONSOLE_ADD_SYMBOL (SDL_StopTextInput),
@@ -1653,6 +1655,22 @@ class Widget : public SignalEmitter {
1653
1655
1654
1656
class Prompt : public Widget {
1655
1657
public:
1658
+ // Holds wrapped lines from input
1659
+ TextEntry entry;
1660
+ // The text of the prompt itself.
1661
+ std::u32string prompt_text;
1662
+ // The input portion of the prompt.
1663
+ std::u32string* input;
1664
+ size_t cursor { 0 }; // position of cursor within an entry
1665
+ // 1x1 texture stretched to font's single character dimensions
1666
+ SDL_Texture* cursor_texture;
1667
+ /*
1668
+ * For input history.
1669
+ * use deque to hold a stable reference.
1670
+ */
1671
+ std::deque<std::u32string> history;
1672
+ int history_index;
1673
+
1656
1674
Prompt (Widget* parent)
1657
1675
: Widget(parent)
1658
1676
{
@@ -1678,6 +1696,7 @@ class Prompt : public Widget {
1678
1696
// sdl_tsd.DestroyTexture(cursor_texture);
1679
1697
}
1680
1698
1699
+ // NOTE: Only called by constructor.
1681
1700
void create_cursor_texture ()
1682
1701
{
1683
1702
cursor_texture = sdl_tsd.CreateTexture (renderer (), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, 1 , 1 );
@@ -1745,16 +1764,18 @@ class Prompt : public Widget {
1745
1764
1746
1765
case SDLK_b:
1747
1766
if (sdl_console::SDL_GetModState () & KMOD_CTRL) {
1748
- cursor = text::prev_word_pos (*input, ( size_t ) cursor);
1767
+ cursor = text::prev_word_pos (*input, cursor);
1749
1768
}
1750
1769
break ;
1751
1770
1752
1771
case SDLK_f:
1753
1772
if (sdl_console::SDL_GetModState () & KMOD_CTRL) {
1754
- cursor = text::next_word_pos (*input, ( size_t ) cursor);
1773
+ cursor = text::next_word_pos (*input, cursor);
1755
1774
}
1756
1775
break ;
1757
1776
case SDLK_c:
1777
+ // FIXME: OutputPane also listens for ctrl-c for copying text.
1778
+ // TODO: Add state for when selecting text
1758
1779
if (sdl_console::SDL_GetModState () & KMOD_CTRL) {
1759
1780
*input += U" ^C" ;
1760
1781
new_interrupted_command ();
@@ -1766,9 +1787,9 @@ class Prompt : public Widget {
1766
1787
void set_command_history (std::deque<std::u32string> saved_history)
1767
1788
{
1768
1789
std::swap (history, saved_history);
1769
- saved_history.clear ();
1770
1790
input = &history.emplace_back (U" " );
1771
1791
history_index = history.size () - 1 ;
1792
+ reset_cursor ();
1772
1793
}
1773
1794
1774
1795
void new_command_input ()
@@ -1780,21 +1801,21 @@ class Prompt : public Widget {
1780
1801
1781
1802
input = &history.emplace_back (U" " );
1782
1803
history_index = history.size () - 1 ;
1783
-
1784
1804
reset_cursor ();
1805
+ wrap_text ();
1785
1806
}
1786
1807
1787
1808
void new_interrupted_command ()
1788
1809
{
1789
1810
emit (InternalEventType::new_input, input);
1790
1811
input->clear ();
1791
1812
reset_cursor ();
1813
+ wrap_text ();
1792
1814
}
1793
1815
1794
1816
void reset_cursor ()
1795
1817
{
1796
1818
cursor = 0 ;
1797
- rebuild = true ;
1798
1819
}
1799
1820
1800
1821
void set_prompt_text (const std::u32string& value)
@@ -1823,7 +1844,7 @@ class Prompt : public Widget {
1823
1844
1824
1845
input = &history.at (history_index);
1825
1846
cursor = input->length ();
1826
- rebuild = true ;
1847
+ wrap_text () ;
1827
1848
}
1828
1849
1829
1850
void put_input_at_cursor (const std::u32string& str)
@@ -1836,7 +1857,7 @@ class Prompt : public Widget {
1836
1857
input->insert (cursor, str);
1837
1858
}
1838
1859
cursor += str.length ();
1839
- rebuild = true ;
1860
+ wrap_text () ;
1840
1861
}
1841
1862
1842
1863
void erase_input ()
@@ -1851,7 +1872,7 @@ class Prompt : public Widget {
1851
1872
input->erase (cursor-1 , 1 );
1852
1873
}
1853
1874
cursor -= 1 ;
1854
- rebuild = true ;
1875
+ wrap_text () ;
1855
1876
}
1856
1877
1857
1878
void move_cursor_left ()
@@ -1874,14 +1895,6 @@ class Prompt : public Widget {
1874
1895
wrap_text ();
1875
1896
}
1876
1897
1877
- void maybe_rebuild ()
1878
- {
1879
- if (rebuild) {
1880
- wrap_text ();
1881
- rebuild = false ;
1882
- }
1883
- }
1884
-
1885
1898
void wrap_text ()
1886
1899
{
1887
1900
entry.text = prompt_text + *input;
@@ -1898,7 +1911,7 @@ class Prompt : public Widget {
1898
1911
1899
1912
// If cursor is the head, nullopt will be returned as it falls outside
1900
1913
// the fragment boundary. Maybe FIXME
1901
- auto & line = [this , cursor_pos]()-> auto & {
1914
+ auto & line = [this , cursor_pos]() -> auto & {
1902
1915
if (auto line_opt = entry.fragment_from_offset (cursor_pos)) {
1903
1916
return line_opt.value ().get ();
1904
1917
} else {
@@ -1929,24 +1942,6 @@ class Prompt : public Widget {
1929
1942
1930
1943
Prompt (const Prompt&) = delete ;
1931
1944
Prompt& operator =(const Prompt&) = delete ;
1932
-
1933
- // Holds wrapped lines from input
1934
- TextEntry entry;
1935
- // The text of the prompt itself.
1936
- std::u32string prompt_text;
1937
- // The input portion of the prompt.
1938
- std::u32string* input;
1939
- // Prompt text/input/cursor was changed flag
1940
- bool rebuild { true };
1941
- size_t cursor { 0 }; // position of cursor within an entry
1942
- // 1x1 texture stretched to font's single character dimensions
1943
- SDL_Texture* cursor_texture;
1944
- /*
1945
- * For input history.
1946
- * use deque to hold a stable reference.
1947
- */
1948
- std::deque<std::u32string> history;
1949
- int history_index;
1950
1945
};
1951
1946
1952
1947
class Scrollbar : public Widget {
@@ -2634,7 +2629,7 @@ class OutputPane : public Widget {
2634
2629
{
2635
2630
for (auto & entry : entries) {
2636
2631
for (auto & frag : entry.fragments ()) {
2637
- if (geometry::is_y_within_bounds (y, frag.coord .y , font->line_height_with_spacing () )) {
2632
+ if (geometry::is_y_within_bounds (y, frag.coord .y , font->line_height )) {
2638
2633
return frag;
2639
2634
}
2640
2635
}
@@ -2650,7 +2645,7 @@ class OutputPane : public Widget {
2650
2645
2651
2646
void copy_selected_text_to_clipboard ()
2652
2647
{
2653
- char32_t sep = U' \n ' ;
2648
+ auto sep = U' \n ' ;
2654
2649
std::u32string clipboard_text;
2655
2650
2656
2651
for (const auto & rect : text_selection.rects ) {
@@ -2697,10 +2692,10 @@ class OutputPane : public Widget {
2697
2692
{
2698
2693
// SDL_RenderSetScale(renderer(), 1.2, 1.2);
2699
2694
sdl_console::SDL_RenderSetViewport (renderer (), &viewport);
2700
- prompt.maybe_rebuild ();
2701
2695
// TODO: make sure renderer supports blending else highlighting
2702
2696
// will make the text invisible.
2703
- render_highlight_selected_text ();
2697
+ if (!text_selection.rects .empty ())
2698
+ render_highlight_selected_text ();
2704
2699
// SDL_SetTextureColorMod(font->texture, 0, 128, 0);
2705
2700
render_prompt_and_output ();
2706
2701
// SDL_SetTextureColorMod(font->texture, 255, 255, 255);
@@ -2749,9 +2744,6 @@ class OutputPane : public Widget {
2749
2744
2750
2745
void render_highlight_selected_text ()
2751
2746
{
2752
- if (text_selection.rects .empty ())
2753
- return ;
2754
-
2755
2747
set_draw_color (renderer (), colors::mediumgray);
2756
2748
for (auto & rect : text_selection.rects ) {
2757
2749
@@ -2831,6 +2823,8 @@ class MainWindow : public Widget {
2831
2823
} else if (e.event == SDL_WINDOWEVENT_FOCUS_LOST) {
2832
2824
has_focus = false ;
2833
2825
} else if (e.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
2826
+ // TODO: Check if cursor is disabled first.
2827
+ SDL_ShowCursor (SDL_ENABLE);
2834
2828
has_focus = true ;
2835
2829
}
2836
2830
});
0 commit comments